/**
 * city基础接口
 */
const AV = require("leanengine");
const BaseService = require("./baseService");
class Service extends BaseService {
  constructor() {
    super("User");
  }

  async register(ctx) {
    const user = new AV.User();
    user.set("username", ctx.params.username);
    user.set("password", ctx.params.password);
    try {
      const res = await user.save();
      return {
        code: "666",
        ...res
      };
    } catch (error) {
      return {
        code: 500,
        msg: error
      };
    }
  }

  async add(ctx) {
    const user = new AV.User();
    user.set("username", ctx.params.username);
    user.set("password", ctx.params.password);
    try {
      const res = await user.save();
      return {
        code: "666",
        ...res
      };
    } catch (error) {
      return {
        code: 500,
        msg: error
      };
    }
  }

  // 验证码注册
  async registerByCode(ctx) {
    const { phone, smsCode } = ctx.params;
    if (!phone || !smsCode) {
      return {
        code: "602",
        msg: "请输入手机号码或验证码"
      };
    }

    try {
      let res = await AV.User.signUpOrlogInWithMobilePhone(phone, smsCode);
      return {
        code: "666",
        msg: "登录成功",
        data: res
      };
    } catch (error) {
      return {
        code: "602",
        msg: error.msg
      };
    }
  }

  // 获取验证码
  async getSmsCode(ctx) {
    let { phone } = ctx.params;
    if (!phone) {
      return {
        code: "602",
        msg: "请输入电话号码"
      };
    }
    try {
      await AV.Cloud.requestSmsCode({
        mobilePhoneNumber: String(phone),
        name: "点时短信验证",
        op: "获取验证码",
        ttl: 10 // 验证码有效时间为 10 分钟
      });
      return {
        code: "666",
        msg: "验证码已发送",
        data: {
          phone
        }
      };
    } catch (error) {
      return {
        code: "602",
        msg: error.msg
      };
    }
  }

  async getList(ctx) {
    let query = new AV.Query("User");
    try {
      let result = await query.find();
      return result;
    } catch (error) {}
  }

  async checkLogin(ctx) {
    var currentUser = AV.User.current();
    if (currentUser) {
      return {
        code: "666",
        data: currentUser
      };
    } else {
      return {
        code: "602",
        msg: "请先登录"
      };
    }
  }

  async login(ctx) {
    const { username, password } = ctx.params;
    let result = this.checkAttr(["username", "password"], ctx.params);
    if (result) return result;
    try {
      const res = await AV.User.logIn(username, password);
      return {
        code: "666",
        data: {
          role: username === "admin" ? "01" : "02",
          token: res._sessionToken,
          username: res.get("username")
        }
      };
    } catch (error) {
      return this.errInfo(error);
    }
  }

  async logout(ctx) {
    AV.User.logOut();
    return {
      code: "666",
      msg: "登出成功"
    };
  }

  async info(ctx) {
    const token = ctx.params.token;
    if (token) {
      return {
        code: "666",
        data: {
          roles: ["admin"],
          name: "admin",
          avatar:
            "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif"
        }
      };
    } else {
      return {
        websocket: true,
        origins: ["*:*"],
        cookie_needed: false,
        entropy: 813046033
      };
    }
  }
}

const service = new Service();
module.exports = service;
